package com.jp.knowledge.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;

import com.jp.knowledge.R;

/**
 * Created by jiayen on 16/10/12.
 * 删除原因dialog
 */

public class DelResonPopuWindow extends PopupWindow implements View.OnClickListener {

    private DelDone delDone;
    private  Context context;
    private View conentView;
    private View shadeView;
    public DelResonPopuWindow(Activity context) {
        super(context);
        this.context=context;
//        context.getWindow().setBackgroundDrawable(context.getResources().getDrawable(R.color.hafl_translate));

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.toutiao_select_dialog, null);
        int h = context.getWindowManager().getDefaultDisplay().getHeight();
        int w = context.getWindowManager().getDefaultDisplay().getWidth();
        // 设置SelectPicPopupWindow的View
        this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        int width = (int) (d.widthPixels*0.8);
        this.setWidth(width);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 刷新状态
        this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x90000000);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        this.setBackgroundDrawable(dw);
        this.setAnimationStyle(android.R.style.Animation_Dialog);

        Window window = context.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        window.setBackgroundDrawableResource(R.color.hafl_translate); //设置对话框背景为透明
//        layoutParams.alpha = 0.8f;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(layoutParams);

        init();

    }

    public void setDelDone(DelDone delDone) {
        this.delDone = delDone;
    }



    private void init(){
        conentView.findViewById(R.id.old_btn).setOnClickListener(this);
        conentView.findViewById(R.id.bad_btn).setOnClickListener(this);
        conentView.findViewById(R.id.looked_btn).setOnClickListener(this);
        conentView.findViewById(R.id.not_like_btn).setOnClickListener(this);
    }

    /**
     *
     *
     * closeType	0	不感兴趣
     closeType	1	重复旧闻
     closeType	2	内容质量差
     closeType	3	来源
     closeType	4	标签
     closeType	5	已看过
     * @param view
     */

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.old_btn:
                    handReson(1);
                break;
            case  R.id.bad_btn:
                handReson(2);
                break;
            case  R.id.looked_btn:
                handReson(5);
                break;
            case  R.id.not_like_btn:
                handReson(0);
                break;
            default:
                break;
        }
    }

    private void handReson(int type){
        if(delDone!=null)delDone.done(type);
    }

    public void setShadeView(View shadeView) {
        this.shadeView = shadeView;
    }

    @Override
    public void showAsDropDown(View anchor) {
        if(shadeView!=null)
        shadeView.setVisibility(View.VISIBLE);
        super.showAsDropDown(anchor);
    }

    @Override
    public void dismiss() {
        if(shadeView!=null)
        shadeView.setVisibility(View.GONE);
        super.dismiss();
    }

    public interface  DelDone{
         void done(int type);
    }
}
